package com.api.shopapi.mapper;

import com.api.shopapi.entity.ArticleClass;
import com.api.shopapi.entity.TrainClass;
import com.api.shopapi.entity.dto.ArticleClassDTO;
import com.api.shopapi.entity.dto.TrainClassDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface TrainClassMapper {

    @Insert("insert into train_class" +
            " (TrainClassCode," +
            "ParentTrainClassCode," +
            "Name," +
            "Icon," +
            "Sort," +
            "Status," +
            "GovernmentCode" +
            ") values " +
            "(#{trainClass.TrainClassCode}," +
            "#{trainClass.ParentTrainClassCode}," +
            "#{trainClass.Name}," +
            "#{trainClass.Icon}," +
            "#{trainClass.Sort}," +
            "#{trainClass.Status}," +
            "#{trainClass.GovernmentCode})")
    int insert(@Param("trainClass") TrainClass trainClass);


    @Update("update train_class set" +
            " ParentTrainClassCode=#{trainClass.ParentTrainClassCode}," +
            "Name=#{trainClass.Name}," +
            "Icon=#{trainClass.Icon}," +
            "Sort=#{trainClass.Sort}," +
            "Status=#{trainClass.Status} " +
            "where TrainClassCode=#{trainClass.TrainClassCode}")
    int update(@Param("trainClass") TrainClass trainClass);

    @Select("select * from train_class where trainClassCode=#{trainClassCode} limit 1")
    TrainClass getByTrainClassCode(@Param("trainClassCode") String trainClassCode);

    @Select("select * from train_class where parentTrainClassCode=#{parentTrainClassCode} and status =1 order by Sort desc")
    List<TrainClass> getListByParentTrainClassCode(@Param("parentTrainClassCode") String parentTrainClassCode);


    //查询父级Code下所有子集（包含状态被禁用的）
    @Select("select * from train_class" +
            " where parentTrainClassCode=#{parentTrainClassCode} order by Sort desc")
    List<TrainClass> getAllListByParentTrainClassCode(@Param("parentTrainClassCode") String parentTrainClassCode);


    //查看政府下所有状态正常的一级分类
    @Select("select * from train_class" +
            " where parentTrainClassCode= '00000000-0000-0000-0000-000000000000' " +
            " and GovernmentCode=#{governmentCode} " +
            " and status = 1" +
            " order by Sort desc")
    List<TrainClassDTO> getFirstClassList(@Param("governmentCode") String governmentCode);

    @Select("<script>" +
            "select * from train_class" +
            " where GovernmentCode = #{governmentCode} " +
            " and parentTrainClassCode = '00000000-0000-0000-0000-000000000000' " +
            "<if test ='name !=null and name!=\"\" '>" +
            " and Name like concat('%',#{name},'%') " +
            "</if>" +
            " order by Sort desc " +
            "</script>")
    List<TrainClassDTO> getPage(@Param("name") String name, @Param("governmentCode") String governmentCode);



}
